package com.zxy.ziems.server.storage.service.imp;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.ziems.common.pojo.MeasureDataDayBean;
import com.zxy.ziems.server.energy.mapper.DeviceMeasureDataDayMapper;
import com.zxy.ziems.server.energy.mapper.DeviceMeasureDataHistoryMapper;
import com.zxy.ziems.server.energy.pojo.entity.DeviceMeasureDataDayEntity;
import com.zxy.ziems.server.energy.pojo.entity.DeviceMeasureDataHistoryEntity;
import com.zxy.ziems.server.mybatis.core.service.ServiceImplX;
import com.zxy.ziems.server.storage.service.MeasureDataDayStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MeasureDataDayStorageServiceImpl extends ServiceImplX<DeviceMeasureDataDayMapper, DeviceMeasureDataDayEntity>
        implements MeasureDataDayStorageService {

    @Autowired
    DeviceMeasureDataHistoryMapper deviceMeasureDataHistoryMapper;

    /**
     * 每日数据存储到day表中和history表中
     * @param list
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void storageMeasureDataDayList(List<MeasureDataDayBean> list) {
        if (CollectionUtil.isEmpty(list)) {
            return;
        }

        List<DeviceMeasureDataDayEntity> dayEntityList = new ArrayList<>();
        List<DeviceMeasureDataHistoryEntity> historyEntityList = new ArrayList<>();
        list.forEach(dataDay -> {
            DeviceMeasureDataDayEntity dayEntity = toDataDayEntity(dataDay);
            DeviceMeasureDataHistoryEntity historyEntity = toDataHistoryEntity(dataDay);
            dayEntityList.add(dayEntity);
            historyEntityList.add(historyEntity);
        });
        baseMapper.batchSaveEntityList(dayEntityList);
        deviceMeasureDataHistoryMapper.batchSaveEntityList(historyEntityList);
    }

    /**
     * MeasureDataDayBean -> DeviceMeasureDataDayEntity
     */
    private DeviceMeasureDataDayEntity toDataDayEntity(MeasureDataDayBean dataDay) {
        DeviceMeasureDataDayEntity entity = new DeviceMeasureDataDayEntity();
        entity.setDeviceId(dataDay.getDeviceId());
        entity.setDataTypeId(dataDay.getDataTypeId());
        entity.setMeasureValue(new BigDecimal(dataDay.getMeasureValue()));
        if (StrUtil.isBlank(dataDay.getIncrementValue())) {
            dataDay.setIncrementValue("0");
        }
        entity.setIncrementValue(new BigDecimal(dataDay.getIncrementValue()));
        entity.setIncrementCost(dataDay.getIncrementCost());
        entity.setMeasureTime(dataDay.getMeasureDate());
        entity.setUnitId(dataDay.getUnitId());
        return entity;
    }

    /**
     * MeasureDataDayBean -> DeviceMeasureDataHistoryEntity
     */
    private DeviceMeasureDataHistoryEntity toDataHistoryEntity(MeasureDataDayBean dataDay) {
        DeviceMeasureDataHistoryEntity entity = new DeviceMeasureDataHistoryEntity();
        entity.setDeviceId(dataDay.getDeviceId());
        entity.setDataTypeId(dataDay.getDataTypeId());
        entity.setMeasureValue(new BigDecimal(dataDay.getMeasureValue()));
        entity.setMeasureTime(dataDay.getMeasureTime());
        entity.setUnitId(dataDay.getUnitId());
        return entity;
    }
}
